package com.example.qimo.alarm;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.Toast;

import com.example.qimo.R;
import com.example.qimo.utils.Music;

import java.util.Calendar;

public class AlarmActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_nearby);
        final TimePicker timePicker=findViewById(R.id.time);  //获取时间拾取组件
        Button button=findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {   //给'设置闹钟'按钮设置监听
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(AlarmActivity.this,alarmActivity2.class);
                PendingIntent pend=PendingIntent.getActivity(AlarmActivity.this,0,intent,0); //显示闹钟，alarmActivity
                AlarmManager alarm= (AlarmManager) getSystemService(Context.ALARM_SERVICE);       // 通过Context.ALARM_SERVICE获取AlarmManager对象
                Calendar calendar =Calendar.getInstance();                     //获取日历对象
                calendar.set(Calendar.HOUR_OF_DAY,timePicker.getHour());       //利用时间拾取组件timePicker得到要设定的时间
                calendar.set(Calendar.MINUTE,timePicker.getMinute());
                calendar.set(Calendar.SECOND,0);
                alarm.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pend);     //设定闹钟
                Toast.makeText(getApplicationContext(), "设置成功", Toast.LENGTH_LONG).show();
                //AlarmManager.ELAPSED_REALTIME 在指定延迟后提醒
                //AlarmManager.ELAPSED_REALTIME_WAKEUP 在指定延迟后提醒,并唤醒系统
                //AlarmManager.RTC     在指定时间提醒

            }
        });
    }

}
